import * as api from '../service'
import cookie from 'js-cookie'

import storeExtend from '@/utils/storeExtend'
import grid from '@/store/common/grid'

export default storeExtend(grid(api), {
	actions: {
		async DoFetchRoleAuth({ dispatch }, payload) {
			const { roleId, pid } = payload
			const resource = await api.fetchResources();//获取资源
			const columns = await api.fetchColumns();//获取权限表头
			if (resource && columns) {
				if (resource.httpCode === 200 && columns.httpCode === 200) {
					//获取权限
					const authRes = await api.queryAuth({ roleId, pid })
					if (authRes) {
						if (authRes && authRes.httpCode === 200) {
							const authData = authRes.data
							const treeData = resource.data
							const colsData = columns.data
							return { authData, treeData, colsData }
						}
					}
				}
			}
			return null
		},
		async fetchAuthList({ commit }, payload) {
			const { roleId, pid } = payload
			const response = await api.queryAuth({ roleId, pid })
			if (response) {
				const { data } = response;
				return data
			}
			return null
		},
		async DoAuth({ commit }, payload) {
			return api.doAuth(payload);
		}
	}
})
